package com.example.administrator.personalmail.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.api.Contants;
import com.example.administrator.personalmail.greendao.AddressDao;
import com.example.administrator.personalmail.model.Address;
import com.example.administrator.personalmail.widget.ClearEditText;
import com.example.administrator.personalmail.widget.GreenDaoManager;

import java.io.Serializable;

public class AddNewAddressActivity extends AppCompatActivity {
    private TextView selectAddress;
    private String address;
    private String name;
    private String phone;
    private String addressDetail;
    private ClearEditText edtName;
    private ClearEditText edtPhone;
    private ClearEditText edtAddressDetail;
    private Button btnComplete;
    private Address editAddress;
    private int state = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_add);

        Serializable serializable = getIntent().getSerializableExtra("editData");
        if (serializable != null){
            editAddress = (Address) serializable;
        }

        selectAddress = findViewById(R.id.txt_address_selcet);
        selectAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddNewAddressActivity.this,SelectAddressActivity.class);
                startActivityForResult(intent,1);
            }
        });

        edtName = findViewById(R.id.edittxt_consignee);
        edtPhone = findViewById(R.id.edittxt_phone_new);
        edtAddressDetail = findViewById(R.id.edittxt_add);

        if (editAddress!=null){
            edtName.setText(editAddress.getName());
            name = editAddress.getName();
            edtPhone.setText(editAddress.getPhone());
            phone = editAddress.getPhone();
            selectAddress.setText(editAddress.getAddress());
            address = editAddress.getAddress();
            edtAddressDetail.setText(editAddress.getAddressDetail());
            addressDetail = editAddress.getAddressDetail();

            state = 1;
        }

        btnComplete = findViewById(R.id.btn_address_addover);
        btnComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkNull()){
                    if (state == 0){
                        setResult(Contants.REQUEST_SUCCESS);
                        insertdata();
                        finish();
                    }else {
                        setResult(Contants.EDIT_ADDRESS_SUCCESS);
                        Address adr = new Address(editAddress.getID(),name,phone,address,addressDetail);
                        getAddressDao().update(adr);
                        finish();
                    }

                }
            }
        });
    }

    public AddressDao getAddressDao()
    {
        return GreenDaoManager.getInstance().getSession().getAddressDao();
    }

    private void insertdata() {
        //插入数据
        Address insertData = new Address(null, name, phone, address,addressDetail);
        getAddressDao().insertOrReplace(insertData);
    }

    private boolean checkNull(){
        name = edtName.getText().toString();
        phone = edtPhone.getText().toString();
        addressDetail = edtAddressDetail.getText().toString();
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(address) || TextUtils.isEmpty(addressDetail)){
            Toast.makeText(AddNewAddressActivity.this,"请完整填写所有信息",Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Contants.REQUEST_SUCCESS){
            address = data.getStringExtra("address");
            selectAddress.setText(address);

        }

    }
}
